﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using AllTalk.AllTalkProxyService;

namespace AllTalk
{
    public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            this.FriendViewModels = new ObservableCollection<FriendViewModel>();
        }

        public ObservableCollection<FriendViewModel> FriendViewModels { get; private set; }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        internal void LoadFriendsList(string rosterXml, string myOwnJid)
        {
            XDocument rosterElement = XDocument.Parse(rosterXml);
            IEnumerable<XElement> friendElements = rosterElement.Descendants(NamespaceConstants.RosterNamespace + "item");
            foreach (XElement friendNode in friendElements)
            {
                Friend friend = new Friend(friendNode);
                if (friend.Email != myOwnJid.Substring(0, myOwnJid.IndexOf('/')))
                {
                    App.DataModel.Friends.Add(friend.Email, friend);
                }
            }
        }

        internal void LoadPresence(IEnumerable<string> presenceXmls)
        {
            foreach (string presenceResponse in presenceXmls)
            {
                if (presenceResponse.Length <= 1)
                    return;

                XElement presenceXml = XElement.Parse(presenceResponse);

                Jid jid = new Jid(presenceXml.Attribute("from").Value);
                Friend friend;
                if (!App.DataModel.Friends.TryGetValue(jid.UserNameDomain, out friend))
                {
                    continue;
                }
                Presence originalPresence = friend.Presence;
                friend.UpdateFriendPresence(jid, presenceXml);
                if (originalPresence == Presence.Unavailable && friend.Presence != Presence.Unavailable)
                {
                    FriendViewModel viewModel = new FriendViewModel(friend);
                    this.FriendViewModels.Add(viewModel);
                }
            }
        }

        internal void LoadMessages(ObservableCollection<string> observableCollection)
        {
            foreach(string message in observableCollection)
            {
                ChatMessage chatMessage = new ChatMessage(XElement.Parse(message, LoadOptions.None));
                ChatViewModel viewModel = null;
                string senderEmail = chatMessage.Sender.UserNameDomain;
                if (!App.ChatViewModels.TryGetValue(senderEmail, out viewModel))
                {
                    // TODO: If the senderEmail does not exist in the friends list.
                    viewModel = new ChatViewModel(App.DataModel.Friends[senderEmail]);
                    App.ChatViewModels.Add(senderEmail, viewModel);
                }
                viewModel.ChatMessageViewModels.Add(new ChatMessageViewModel(chatMessage));
                viewModel.Friend.ActiveJid = chatMessage.Sender;
            }
        }

        internal void LoadVcards(ObservableCollection<Vcard> vcards)
        {
            foreach (Vcard vcard in vcards)
            {
                Friend friend = App.DataModel.Friends[vcard.UserId];
                XElement vcardElement = XElement.Parse(vcard.Xml);
                XElement photoElement = vcardElement.Element(NamespaceConstants.VCardNamespace + "PHOTO");
                if (photoElement != null)
                {
                    friend.LoadPicture(photoElement);
                }
            }
        }
    }
}